package com.xavier.dong.nio;

import java.nio.ByteBuffer;

/**
 * @author XavierDong
 **/
public class ReadOnlyBuffer {

    public static void main(String[] args) {

        // 创建一个 Buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(64);

        for (int i = 0; i < byteBuffer.capacity(); i++) {
            byteBuffer.put(((byte) i));
        }

        // 进行翻转
        byteBuffer.flip();

        // 得到一个 只读的 Buffer
        ByteBuffer readOnlyBuffer = byteBuffer.asReadOnlyBuffer();

        System.out.println(readOnlyBuffer.getClass());

        while (readOnlyBuffer.hasRemaining())
            System.out.println(readOnlyBuffer.get());

        // 抛出 ReadOnlyBufferException 抛出异常
        readOnlyBuffer.put(((byte) 100));


    }
}
